<?php


class RotatorController extends Zend_Controller_Action
{

     protected $_flashMessenger = null;

    function init()
    {
        $usersession = new \Zend_Session_Namespace("usersession");


                       if (!($usersession->general=="generalisimus")){

                           $this->_redirect('/getin');
                       }

        if ($this->_request->isXmlHttpRequest()) {
            $this->_helper->layout()->disableLayotu();
        }

        $this->_helper->layout()->setLayout('admin-layout');

        $this->_flashMessenger = new Lib_Flash();
           
    }
public function grid($qb, $id = '')
    {
        $view = new Zend_View();
        $view->setEncoding('UTF-8');

        $config = new Zend_Config_Ini(APPLICATION_PATH . '/grids/grid.ini', 'production');

        $grid = Bvb_Grid::factory('Table', $config, $id);

        $grid->setEscapeOutput(true);
        $grid->setExport(array('csv', 'excel', 'pdf'));
        $grid->setView($view);
        $grid->setDeleteConfirmationPage(true);
        $gridManager = new \Managers\Grid();
        $em = $gridManager->em();
        $source = new Bvb_Grid_Source_Doctrine2($qb, $em);
        $grid->setSource($source);
        $grid->updateColumn('id', array('hidden' => true));

        $form = new Bvb_Grid_Form();
        $form->setAdd(true)->setEdit(true)->setDelete(true)->setAddButton(true);
        $grid->setForm($form);

        return $grid;
    }



    function editAction(){

        $gridManager=new \Managers\Grid();
        $qb= $gridManager->editRotator();
        $grid = $this->grid($qb);

        $this->view->form = $grid->deploy();




    }
   function  editpublicAction()
 {
     $form = new App_Form_Rotator_Form();
     $this->view->form = $form;
     $id=$this->_request->getParam('id');
     $rotatorManager = new Managers\Rotator();
     $pic = $rotatorManager->getOne($id);
     $this->view->pic=$pic;


     if ($this->_request->isPost())
         {

             $params = $this->_request->getParams();
             if ($_FILES['img']['name'])


             {
                 try {
                                      $form->img->receive();

                                      $phManager = new \Managers\Gallery();
                                      $object = $phManager->savePublic($id,'',$params['public']);

                                      $file_path = $form->img->getFileName();
                                      $img_value = $form->img->getValue();

                                      if (!empty($img_value)) {
                                          $image_url = $this->getImageUrlForGallery($object->getId(), $file_path);
                                          $phManager->resizer('unused', dirname(APPLICATION_PATH) . '/public' . $image_url, $file_path, 250);
                                          $phManager->setImageRotator($object->getId(), $image_url);
                                      }

                                      if ($params['public'])
                                      {
                                          $this->_flashMessenger->addMessage('Photo successfully edited.', Lib_Flash::NOTICE, Lib_Flash::LATER);
                                          $this->_redirect('/rotator');
                                      }
                            else { $this->_flashMessenger->addMessage('Photo successfully edited, but not publish.', Lib_Flash::NOTICE, Lib_Flash::LATER);}
                               }
                          catch (Exception $e)
                              {
                                      $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::NOW);
                              }

             }
             else {

                 try{
                 $phManager = new \Managers\Gallery();
                 $phManager->savePublic($id,$pic[0]['img'],$params['public']);
                 $this->_flashMessenger->addMessage('Photo successfully edited.', Lib_Flash::NOTICE, Lib_Flash::LATER);
                                                               $this->_redirect('/rotator');
                 }
                 catch (Exception $e)
                                               {
                                                       $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::NOW);
                                               }
             }

     }


 }

    function indexAction(){

    }
    function addAction()
       {

           $form = new App_Form_Rotator_Form();

           if ($this->_request->isPost())
           {
               $params = $this->_request->getParams();
//               print_r($_FILES); die;

         try {
                       $form->img->receive();

                       $phManager = new \Managers\Gallery();
                       $object = $phManager->pictureRotator($params['public']);

                       $file_path = $form->img->getFileName();
                       $img_value = $form->img->getValue();

                       if (!empty($img_value)) {
                           $image_url = $this->getImageUrlForGallery($object->getId(), $file_path);
                           $phManager->resizer('unused', dirname(APPLICATION_PATH) . '/public' . $image_url, $file_path, 250);
                           $phManager->setImageRotator($object->getId(), $image_url);
                       }

                       if ($params['public'])
                       {
                           $this->_flashMessenger->addMessage('Photo successfully added.', Lib_Flash::NOTICE, Lib_Flash::LATER);
                           $this->_redirect('/rotator');
                       }
             else { $this->_flashMessenger->addMessage('Photo successfully added, but not publish.', Lib_Flash::NOTICE, Lib_Flash::LATER);}
                }
           catch (Exception $e)
               {
                       $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::NOW);
                   }

           }

           $this->view->form = $form;

       }
    /**
     * Возвращает адрес картинки для новости.
     * @param $id
     * @param $file_path путь к загруженному файлу.
     * @return string путь к файлу для хранения в базе (относительно папки public)
     */

         private function getImageUrlForGallery($id, $file_path)
    {
        $prefix = '/images/rotator/';

        $new_name = $id . substr(md5(time()), 0, 6);
        $ext = pathinfo($file_path, PATHINFO_EXTENSION);

        $result = preg_replace('/\/{2,}/', '/', $prefix . $new_name . '.' . $ext);
        return $result;
    }

}